﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using QuickApp.Core;
using EfSearchModel.Model;
using QuickApp.MVCControl;
using Resto.CostControl.Core;
using QuickApp.MVCControl.Form;

namespace Resto.CostControl.Web.Controllers
{
    public class MaterialDeliveryController : Controller
    {
        private readonly IRepository<MaterialPurchase> _Repo;
        private readonly IRepository<MaterialPurchaseItem> _RepoItem;
        private readonly IRepository<DishOrderItem> _RepoWeek;
        private readonly IUnitOfWork _UnitOfWork;

        public MaterialDeliveryController(IRepository<MaterialPurchase> repo, IRepository<MaterialPurchaseItem> repoDM, IRepository<DishOrderItem> RepoWeek, UnitOfWork unitOfWork)
        {
            _Repo = repo;
            _RepoItem = repoDM;
            _RepoWeek = RepoWeek;
            _UnitOfWork = unitOfWork;
        }

        public ActionResult Index()
        {
            return View();
        }

        [HttpPost]
        public JsonResult Index(DWZPageInfo pageInfo, DWZQueryModel where)
        {
            var list = from c in _RepoWeek.FindAll() group c by new {c.SiteId,c.Date} into g select new 
            {
                SiteId = g.Key.SiteId,
                Date = g.Key.Date
            };
            List<DishOrderItem> items = new List<DishOrderItem>();
            foreach (var item in list.ToList())
            {
                items.Add(new DishOrderItem 
                {
                    SiteId = item.SiteId,
                    Date = item.Date,
                });
            }
            //var list = _RepoWeek.GetPageable(pageInfo.ToPageInfo(),where);
            var fdata = pageInfo.ToFlexiGridData<DishOrderItem>(items, items.Count);
            return Json(fdata);
        }

        public ActionResult Edit(string Id, string SiteId, string Date)
        {
            MaterialPurchase data = null;
            //if (!string.IsNullOrWhiteSpace(Id))
            //{
            //    data = _Repo.Find(Id);
            //}
            //else
            //{
                data = new MaterialPurchase();
                data.Id = Guid.NewGuid().ToString();
                data.OrderDate = DateTime.Parse(Date);
                ViewBag.SiteId = SiteId;
            //}
            return View(data);
        }

        [HttpPost]
        public JsonResult Save(MaterialPurchase data, IList<MaterialPurchaseItem> MaterialList)
        {
            if (ModelState.IsValid)
            {
                try
                {
                    var ItemDesc = "";
                    if (MaterialList != null)
                    {
                        foreach (var m in MaterialList)
                        {
                            ItemDesc += m.Name+",";
                            m.OrderId = data.Id;
                            m.MaterialId = m.Id;
                            m.Id = Guid.NewGuid().ToString();
                            m.State = "xiadan";
                            _RepoItem.Insert(m);
                        }
                    }
                    data.ItemDesc = ItemDesc.Trim(',');
                    if (_Repo.Find(data.Id) == null)
                    {
                        data.OrderState = "xiadan";
                        _Repo.Insert(data);
                    }
                    else
                    {
                        data = _Repo.Find(data.Id);
                        TryUpdateModel(data);

                        _RepoItem.DeleteAll(m => m.OrderId == data.Id);
                    }

                    _UnitOfWork.Commit();
                }
                catch (Exception ex)
                {
                    throw ex;
                }
            }

            var result = new JsonReturnMessages
            {
                IsSuccess = true,
                Msg = "操作成功",
            };
            return Json(result);
        }



        [HttpPost]
        public JsonResult Delete(string id)
        {
            JsonReturnMessages msg = new JsonReturnMessages();
            if (!string.IsNullOrEmpty(id))
            {
                try
                {
                    _Repo.Delete(id);
                    _UnitOfWork.Commit();
                    msg.IsSuccess = true;
                    msg.Msg = "操作成功";
                }
                catch
                {
                    msg.IsSuccess = false;
                    msg.Msg = "操作失败,请稍后重试！";
                }
            }
            else
            {
                msg.IsSuccess = false;
                msg.Msg = "参数错误";
            }
            return Json(msg);
        }

        [HttpPost]
        public JsonResult MaterialList(string SiteId, string ReportDate, DWZPageInfo pageInfo, DWZQueryModel where)
        {
            var items = _RepoWeek.FindAll(e => e.SiteId == SiteId && e.Date.ToString("yyyy-MM.dd") == ReportDate);
            List<MaterialPurchaseItem> list = new List<MaterialPurchaseItem>(); 
            foreach (var item in items)
            {
                list.Add(new MaterialPurchaseItem 
                { 
                    MaterialId = item.Id, 
                    Code = item.Code,
                    Name = item.Name,
                    Price = item.DishPrice ,
                    Category = item.Category,
                    OrderCount = item.Count,
                });
            }
            var fdata = pageInfo.ToFlexiGridData<MaterialPurchaseItem>(list, list.Count);
            return Json(fdata);
        }

        public ActionResult EditList()
        {
            return View();
        }

        [HttpPost]
        public JsonResult EditList(string parentId, DWZPageInfo pageInfo, DWZQueryModel where)
        {
            where.AddItem("OrderId", parentId == "" ? null : parentId);
            var list = _RepoItem.GetPageable(pageInfo.ToPageInfo(), where);
            var fdata = pageInfo.ToFlexiGridData<MaterialPurchaseItem>(list.Page.ToList(), list.Total);
            return Json(fdata);
        }

        [HttpPost]
        public JsonResult DeleteItem(string Id)
        {
            JsonReturnMessages msg = new JsonReturnMessages();
            if (!string.IsNullOrEmpty(Id))
            {
                try
                {
                    _RepoItem.Delete(Id);
                    _UnitOfWork.Commit();
                    
                }
                catch
                {
                    msg.IsSuccess = false;
                    msg.Msg = "操作失败,请稍后重试！";
                }
            }
            else
            {
                msg.IsSuccess = false;
                msg.Msg = "参数错误";
            }
            return Json(msg);
        }
    }
}

